package controller;

import entity.MsgResult;
import entity.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.SelectAllUserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    private SelectAllUserService selectAllUserService;

    @RequestMapping("/login")
    public String login() {
        return "Login/login2";
    }

    @RequestMapping("/loginIn")
    @ResponseBody
    public MsgResult loginIn(user user, HttpServletRequest request) {
        MsgResult msgResult = new MsgResult();
        HttpSession session = request.getSession();
        List<user> users = selectAllUserService.selectAllUser();
        for (user us : users) {
            if (us.getUserName().equals(user.getUserName())
                    && us.getPassword().equals(user.getPassword())
                    && us.getRole().equals(user.getRole())) {
                session.setAttribute("u",user);
                msgResult.setFlag(1);
                msgResult.setMsg("登录成功");
                return msgResult;
            }
        }
        msgResult.setFlag(0);
        msgResult.setMsg("登录失败");
        return msgResult;
    }


}
